<template>
  <!-- 实现了双向绑定
    父到子
    子到父
  -->
  <el-dialog title="新增员工" :visible.sync="show" @close="close">
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="姓名" prop="username" :label-width="formLabelWidth">
        <el-input v-model="form.username" autocomplete="off" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile" :label-width="formLabelWidth">
        <el-input v-model="form.mobile" autocomplete="off" />
      </el-form-item>
      <el-form-item
        label="入职时间"
        prop="timeOfEntry"
        :label-width="formLabelWidth"
      >
        <el-date-picker
          v-model="form.timeOfEntry"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item
        label="聘用形式"
        prop="formOfEmployment"
        :label-width="formLabelWidth"
      >
        <el-select
          v-model="form.formOfEmployment"
          placeholder="请选择活动区域"
          @blur="$refs.form.validateField('formOfEmployment')"
          @change="$refs.form.validateField('formOfEmployment')"
        >
          <el-option
            v-for="item in enumObj.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item
        label="工号"
        prop="workNumber"
        :label-width="formLabelWidth"
      >
        <el-input v-model="form.workNumber" autocomplete="off" />
      </el-form-item>
      <el-form-item
        label="部门"
        prop="departmentName"
        :label-width="formLabelWidth"
      >
        <el-input
          v-model="form.departmentName"
          readonly
          autocomplete="off"
          @focus="handleFocus"
          @click.native="handleFocus"
          @blur="handleBlur"
        />
        <el-tree
          v-if="treeShow"
          :expand-on-click-node="false"
          default-expand-all
          :data="treeData"
          :props="defaultProps"
          @node-click="handleNodeClick"
          @mousedown.native.prevent=""
        />
      </el-form-item>
      <el-form-item
        label="转正时间"
        prop="correctionTime"
        :label-width="formLabelWidth"
      >
        <el-date-picker
          v-model="form.correctionTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button
        type="primary"
        @click="save"
      >确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
// 导入枚举库
import enumObj from '@/utils/constant/employees'
// 导入获取部门列表的接口
import { deptListAPI } from '@/api/departments'
import { addEmpAPI } from '@/api/employees'
import { array2Tree } from '@/utils'
export default {
  data() {
    return {
      enumObj: enumObj,
      show: false,
      formLabelWidth: '80px',
      form: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      treeData: [],
      treeShow: false,
      rules: {
        username: [
          { required: true, message: '请输入此内容', trigger: 'blur' }
        ],
        mobile: [{ required: true, message: '请输入此内容', trigger: 'blur' }],
        formOfEmployment: [
          { required: true, message: '请输入此内容', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '请输入此内容', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '请输入此内容', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '请输入此内容', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请输入此内容', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async handleFocus() {
      if (!this.treeShow) {
        // 显示出部门
        this.treeShow = true
        // 加载部门数据
        const res = await deptListAPI()
        this.treeData = array2Tree(res.data.depts, '')
      }
    },

    // 失去焦点事件
    handleBlur(e) {
      // 判断下一个目标如果不是tree，就隐藏
      // 不管你从哪失去焦点，都应该隐藏
      this.treeShow = false
    },

    // 例如a标签的点击有默认行为是跳转，阻止了就不跳转
    // 对于输入框而言，点击也有默认行为，默认行为就是获得焦点

    // 节点的点击事件
    handleNodeClick(data) {
      // 拿到被点的数据，赋值给输入框
      this.form.departmentName = data.name
      // 隐藏
      this.treeShow = false
      // 直接无条件直接清除校验都可以
      this.$refs.form.clearValidate('departmentName')
    },

    save() {
      // 整个表单校验
      this.$refs.form.validate(async v => {
        if (v) {
          await addEmpAPI(this.form)
          this.$message.success('新增成功')
          // 刷新数据
          this.$emit('reload')
          // 找到父组件
          // console.log(this.$parent)
          // this.$parent.getData()
          // 关闭对话框
          this.show = false
        }
      })
    },

    close() {
      // 清空数据
      this.form = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      // 清空校验
      this.$refs.form.clearValidate()
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-tree-node__expand-icon {
  display: none;
}
</style>
